package kr.hkit.androidcomplete03.ch28;

import java.io.FileOutputStream;

import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class Ch28Main extends Activity {
	
	ProgressDialog progressDlg;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch28main);
		new XmlJsonMake().execute();
	}
	
	public void mOnClick(View v){
		Intent intent = null;
		
		switch(v.getId()){
		
		case R.id.dom_parser:
			intent = new Intent(this, DOMParser.class);
			break;
			
		case R.id.sax_parser:
			intent = new Intent(this, SAXParserEx.class);
			break;
			
		case R.id.xmlpull_parser:
			intent = new Intent(this, XMLPULLParser.class);
			break;
			
		case R.id.json_parser:
			intent = new Intent(this, JSONParser.class);
			break;
		}
		startActivity(intent);
	}
	
	private class XmlJsonMake extends AsyncTask<Void, Void, Void>{
		
		@Override
		protected Void doInBackground(Void... params) {
			Log.d("TAG", "Ch28Main _ doInBackground()");
			try { 
				 FileOutputStream fos = openFileOutput("order.xml",MODE_PRIVATE); 
				 String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" 
				 + "<order>\n" 
				 + " <item Maker=\"Samsung\" Price=\"23000\">Mouse</item>\n" 
				 + " <item Maker=\"LG\" Price=\"12000\">KeyBoard</item>\n" 
				 + " <item Price=\"156000\" Maker=\"Western Digital\">HDD</item>\n" 
				 + "</order>"; 
				 fos.write(str.getBytes()); 
				 fos = openFileOutput("order.json", MODE_PRIVATE); 
				 String Json = "[\n" 
				 + " {\"Product\":\"Mouse\", \"Maker\":\"Samsung\", \"Price\":23000},\n" 
				 + "{\"Product\":\"KeyBoard\", \"Maker\":\"LG\", \"Price\":12000},\n" 
				 + "{\"Product\":\"HDD\", \"Maker\":\"Western Digital\", \"Price\":156000} \n" 
				 + "]"; 
				 fos.write(Json.getBytes()); 
				 fos.close(); 
				 Thread.sleep(3000); 
				 } catch (Exception e) { Log.d("XmlParser", e.getMessage()); } 
				 return null; 
				 } 

		@Override
		protected void onPreExecute() {
			Log.d("TAG", "Ch28Main _ onPreExecute()");
			progressDlg = ProgressDialog.show(Ch28Main.this, "Wait", "order.xml & order.json Making ..."); 
		}

		@Override
		protected void onPostExecute(Void result) {
			Log.d("TAG", "Ch28Main _ onPostExecute()");
			progressDlg.dismiss();
			Toast.makeText(Ch28Main.this, "order.xml & order.json make", Toast.LENGTH_LONG).show();
		}
	}

}


